package com.android;

import java.util.Calendar;

import kankan.wheel.widget.OnWheelClickedListener;
import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.NumericWheelAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends Activity implements
		SeekBar.OnSeekBarChangeListener {
	private TextView tv_h, tv_m, tv_left_h, tv_left_m, tv_alarm_run_h, tv_alarm_run_m;
	private SeekBar sb;
	int curHours;
	int curMinutes;
	private WheelView hours, mins;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		hours = (WheelView) findViewById(R.id.hour);
		hours.setViewAdapter(new NumericWheelAdapter(this, 0, 23));

		mins = (WheelView) findViewById(R.id.mins);
		mins.setViewAdapter(new NumericWheelAdapter(this, 0, 59, "%02d"));
		mins.setCyclic(true);

		// set current time
		final Calendar c = Calendar.getInstance();
		curHours = c.get(Calendar.HOUR_OF_DAY);
		curMinutes = c.get(Calendar.MINUTE);

		hours.setCurrentItem(curHours);
		mins.setCurrentItem(curMinutes);

		OnWheelClickedListener click = new OnWheelClickedListener() {
			public void onItemClicked(WheelView wheel, int itemIndex) {
				wheel.setCurrentItem(itemIndex, true);
			}
		};
		hours.addClickingListener(click);
		mins.addClickingListener(click);

		OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
			public void onScrollingStarted(WheelView wheel) {
			}

			public void onScrollingFinished(WheelView wheel) {
				CalcTime time = new CalcTime(curHours, curMinutes,
						hours.getCurrentItem(), mins.getCurrentItem());

				tv_left_h.setText(String.valueOf(time.getHours()));
				tv_left_m.setText(String.valueOf(time.getMinutes()));
				
				tv_alarm_run_h.setText(String.valueOf(hours.getCurrentItem()));
				tv_alarm_run_m.setText(String.valueOf(mins.getCurrentItem()));
				
				sb.setProgress(time.getDiff());
			}
		};
		hours.addScrollingListener(scrollListener);
		mins.addScrollingListener(scrollListener);

		tv_h = (TextView) findViewById(R.id.text_left_sleep_h);
		tv_m = (TextView) findViewById(R.id.text_left_sleep_m);

		tv_left_h = (TextView) findViewById(R.id.left_sleep_h);
		tv_left_m = (TextView) findViewById(R.id.left_sleep_m);
		
		tv_alarm_run_h = (TextView) findViewById(R.id.alarm_run_h);
		tv_alarm_run_m = (TextView) findViewById(R.id.alarm_run_m);

		sb = (SeekBar) findViewById(R.id.seekbar);
		sb.setOnSeekBarChangeListener(this);				
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		
		int time_h = seekBar.getProgress() / 60;
		int time_m = seekBar.getProgress() % 60;
		if (progress <= 840) {
			findViewById(R.id.time_more14).setVisibility(View.INVISIBLE);
			findViewById(R.id.time_layout).setVisibility(View.VISIBLE);

			tv_h.setText(String.valueOf(time_h));
			tv_m.setText(String.valueOf(time_m));

		} else {
			time_h = 14;
			time_m = 0;
			findViewById(R.id.time_more14).setVisibility(View.VISIBLE);
			findViewById(R.id.time_layout).setVisibility(View.INVISIBLE);
		}

		if (fromUser) {
			tv_left_h.setText(String.valueOf(time_h));
			tv_left_m.setText(String.valueOf(time_m));

			int time_all_h = curHours + time_h;
			int time_all_m = curMinutes + time_m;

			int stand_up_h = 0;
			int stand_up_m = 0;
			if (time_all_h >= 24) {
				stand_up_h = time_all_h - 24;
			} else {
				stand_up_h = time_all_h;
			}
			if (time_all_m >= 60) {
				stand_up_m = time_all_m % 60;
				stand_up_h += time_all_m / 60;
			} else {
				stand_up_m = time_all_m;
			}

			if(stand_up_h == 24) { stand_up_h = 0; }			
			
			hours.setCurrentItem(stand_up_h);
			mins.setCurrentItem(stand_up_m);
			
			tv_alarm_run_h.setText(String.valueOf(hours.getCurrentItem()));
			tv_alarm_run_m.setText(String.valueOf(mins.getCurrentItem()));
		}

	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
	}
	
	public void BtnHandler(View target){
		switch(target.getId()){
		case R.id.btn_off:
			Intent intent = new Intent(getBaseContext(), OffAlarm.class);
			startActivity(intent);
			break;
		}
		
	}
}
